package com.stlm2.core.yun;


import com.stlm2.core.AppConfig;
import com.stlm2.core.common.md5.MD5;
import com.stlm2.util.Convert;
import com.stlm2.util.HttpsClient;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * 云通信
 *
 */
public class Core {
	//主账户ID
//public static final String ACCOUNT_SID = "8a216da856a243ab0156ac1c86e50391";
//	public static final String ACCOUNT_SID = "aaf98f89493217c801493bc7ac4607b8";
//
//	//主账户token
	//public static final String AUTH_TOKEN = "51eed9e500e646908d3cf0b447867e55";
//	public static final String AUTH_TOKEN = "fc8d9e6ce96342c1916fbef3a87eb6b9";
//	
//	//子账户ID
	//public static final String SUB_ACCOUNT_SID = "04d83f4e681011e6997a6c92bf2c165d";
//	public static final String SUB_ACCOUNT_SID = "cf2784a5591411e4823dac853d9f54f2";
//	
//	//子账户token
	//public static final String SUB_AUTH_TOKEN = "a2c1d6e56647c7d19e786cf6c0ede86f";
//	public static final String SUB_AUTH_TOKEN = "ce9828e5cb8a96eb36640afdfe091063";
//	
//	//应用ID
	//public static final String APP_ID = "8a216da856a243ab0156ac1c875d0396";
//	public static final String APP_ID = "8a48b551493219e001493c00f7d6075f";
//	
//	//主账户地址

//	public static final String BASE_ACCOUNT_URL = "https://app.cloopen.com:8883/2013-12-26/Accounts/" + ACCOUNT_SID;
//	
//	//子账户地址

//	public static final String BASE_SUBACCOUNT_URL = "https://app.cloopen.com:8883/2013-12-26/SubAccounts/" + SUB_ACCOUNT_SID;
	
	//主账户ID
	public static final String ACCOUNT_SID = AppConfig.YUN_ACCOUNT_SID;
	
	//主账户token+
	public static final String AUTH_TOKEN = AppConfig.YUN_AUTH_TOKEN;
	
	//子账户ID
	public static final String SUB_ACCOUNT_SID = AppConfig.YUN_SUB_ACCOUNT_SID;
	
	//子账户token
	//
	public static final String SUB_AUTH_TOKEN = AppConfig.YUN_SUB_AUTH_TOKEN;
	
	//应用ID
public static final String APP_ID = AppConfig.YUN_APP_ID;
	
	//请求地址
	private static final String BASE_URL = AppConfig.YUN_URL;
	
	//主账户地址
	public static final String BASE_ACCOUNT_URL = BASE_URL + "/2013-12-26/Accounts/" + ACCOUNT_SID;
	
	//子账户地址
	public static final String BASE_SUBACCOUNT_URL = BASE_URL + "/2013-12-26/SubAccounts/" + SUB_ACCOUNT_SID;

	//成功码
	public static final String SUCC_CODE = "000000";
	
	/**
	 * 签名
	 * @param isSub	是否为子账户请求
	 * @param date
	 * @return
	 */
	private static String sign(boolean isSub, Date date){
		if(isSub)
			return MD5.encryptMD5(SUB_ACCOUNT_SID + SUB_AUTH_TOKEN + Convert.toFullYMDHMSWithNoSplit(date));
		else
			return MD5.encryptMD5(ACCOUNT_SID + AUTH_TOKEN + Convert.toFullYMDHMSWithNoSplit(date));
	}
	
	/**
	 * 授权
	 * @param isSub	是否为子账户请求
	 * @param date
	 * @return
	 */
	private static String auth(boolean isSub, Date date){
		try {
			if(isSub)
				return Base64.encodeBase64String((SUB_ACCOUNT_SID + ":" + Convert.toFullYMDHMSWithNoSplit(date)).getBytes("UTF-8"));
			else
				return Base64.encodeBase64String((ACCOUNT_SID + ":" + Convert.toFullYMDHMSWithNoSplit(date)).getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 发送请求并返回结果
	 * @param url
	 * @param param
	 * @return
	 */
	public static String post(boolean isSub, String url, String param){
		if(StringUtils.isBlank(url) || StringUtils.isBlank(param))
			return null;
		Date date = new Date();
		url += "?sig=" + sign(isSub, date);
		HttpPost post = new HttpPost(url);
		post.addHeader("Content-Type", "application/json;charset=utf-8;");
		post.addHeader("Accept", "application/json;");
		post.addHeader("Authorization", auth(isSub, date));
		post.setEntity(new StringEntity(param, "UTF-8"));
		HttpClient client = HttpsClient.getClient(8883);
		try {
			HttpResponse response = client.execute(post);
			HttpEntity entity = response.getEntity();
			String result = EntityUtils.toString(entity, "UTF-8");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
